clear all
*cap log close
set more off
set seed 603

// other pars -------------
local boot = 250
local Y    = "cite_ny1"
// ------------------------

// construct bootstrap weights -------
use "${temp}/data_extrap", clear 
keep officerid 
duplicates drop 
gen bwt_0 = 1 
forval j = 1/`boot' {
	gen temp1 = rgamma(1,1)
	egen temp2 = sum(temp1)
	gen bwt_`j' = temp1/temp2
	drop temp1 temp2
}
qui compress
save "${temp}/bootwt2", replace 
// -------------------------------------


// setup data with (fixed) optimal bins -----
use "${temp}/optimal_binscatter", clear
qui summ dots_binid 
local bins_count = `r(max)'
gen dummy = 1 
keep dummy dots_binid lef_ rig_
reshape wide lef_ep rig_ep, i(dummy) j(dots_binid)
tempfile temp 
save    `temp'

use "${temp}/data_extrap", clear 
gen dummy = 1 
merge m:1 dummy using `temp', keep(1 3) nogen 

gen bin = . 
forval j = 1/`bins_count' {
	replace bin = `j' if Z>lef_ep`j' & Z<=rig_ep`j'
}
replace bin = 1 if Z==0
replace bin = `bins_count' if Z==1

forval j = 2/`bins_count' {
	gen bin_`j' = (bin==`j')
}
drop dummy lef_* rig_*

gen Y = `Y'
gen D = harsh 
save "${temp}/optimal_fixed_data", replace 
// -----------------------------------------



// initialize bootstrap loop -----------
forval j = 0/`boot' {

	* setup data ------
	use "${temp}/optimal_fixed_data", clear 
	merge m:1 officerid using "${temp}/bootwt2", keep(1 3) nogen 
	gen bwt = bwt_`j'
	drop bwt_*

	* binned regression --------
	reghdfe Y bin_2-bin_`bins_count' [aw=bwt], absorb(totfe)
	local y0 = _b[_cons]
	local y1 = _b[_cons] + _b[bin_`bins_count']

	* get other parameters ----------------
	qui summ Y [aw=bwt]
	local y = `r(mean)'
	qui summ D [aw=bwt]
	local p = `r(mean)'
	reghdfe Y D [aw=bwt]
	local y0d0 = _b[_cons]
	local y1d1 = _b[_cons]+_b[D]

	* store parameters ------------------
	clear 
	set obs 1 
	foreach par in y p y0d0 y1d1 y0 y1 {
		gen `par' = ``par'' 
	}

	* add other parameters -------------
	gen ate = y1-y0 
	gen att = (y-y0)/p 
	gen atu = (y1-y)/(1-p)
	gen y1d0 = (1/(1-p))*y1-(p/(1-p))*y1d1
	gen y0d1 = (1/p)*y0-((1-p)/p)*y0d0
	gen y0d1my0d0 = y0d1-y0d0
	gen attmatu = att-atu 

	* store bootstrap output ------------
	gen iter = `j'
	if `j'==0 {
		save "${temp}/fixedboot", replace
	}
	if `j'>=1 {
		qui append using "${temp}/fixedboot" 
		save "${temp}/fixedboot", replace 
	}

	* progress report ---------------
	di "Iteration `j' of `boot' completed"

}


// compile bootstrap output ------------------
// annoying stuff to format output same as R output
use "${temp}/fixedboot", clear 
keep if iter > 0 
collapse (sd) y-attmatu
tempfile se
save    `se'

use "${temp}/fixedboot", clear 
keep if iter == 0 
drop iter 
tempfile est 
save    `est'

ds
local varlist `r(varlist)'
local parcount: word count `varlist'

clear
set obs `parcount' 
gen par = ""
gen est = . 
gen se  = .
save "${est}/optimal_fixed", replace  

local k=1 
foreach v in `varlist' {

	di "`v'"
	use `est', clear 
	summ `v'
	local est_copy = `r(mean)'
	use `se', clear 
	summ `v'
	local se_copy  = `r(mean)'

	use "${est}/optimal_fixed", clear 
	replace par = "`v'" if _n == `k'
	replace est = `est_copy' if _n == `k'
	replace se  = `se_copy'  if _n == `k'
	save "${est}/optimal_fixed", replace
	local ++k

}

gen ub = est+1.96*se 
gen lb = est-1.96*se
save "${est}/optimal_fixed", replace
rm   "${temp}/optimal_fixed_data.dta"
rm   "${temp}/bootwt2.dta"
rm   "${temp}/fixedboot.dta"
*rm   "${temp}/optimal_binscatter.dta"










